/*
 * Copyright 2007 Jeffrey Palm
 */

import java.io.*;
import java.net.*;
import java.util.*;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

import org.codehaus.bloglines.*;
import org.codehaus.bloglines.exceptions.*;
import com.sun.syndication.feed.synd.*;

abstract class AbstractSearchWithLogin extends AbstractSearch implements RSSCollectionFinder {

  protected String username;
  protected String password;

  public AbstractSearchWithLogin(String username,String password) {
    this.username = username;
    this.password = password;
  }

  public AbstractSearchWithLogin() {
  }

  protected abstract RSSCollection getCollectionRest(Progress p) throws Exception;

  public final RSSCollection getCollection(Progress p) throws Exception {
    username = Config.getInstance().get(getClass().getName()+":"+Props.USERNAME);
    password = Config.getInstance().get(getClass().getName()+":"+Props.PASSWORD);
    LoginInfoFrame login = null;
    boolean save = false;
    do {
      if (login == null) login = new LoginInfoFrame(username,password);
      login.show();
      if (login.isCancelled()) return null;
      username = login.getUsername();
      password = login.getPassword();
      save = login.isChecked();
    } while (Util.isEmpty(username) || Util.isEmpty(password));
    if (save) {
      Config.getInstance().set(getClass().getName()+":"+Props.USERNAME,username);
      Config.getInstance().set(getClass().getName()+":"+Props.PASSWORD,password);
    }
    return getCollectionRest(p);
  }
}